package com.bjsxt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bjsxt.constants.Constants;
import com.bjsxt.domain.Menu;
import com.bjsxt.domain.SimpleUser;
import com.bjsxt.dto.MenuDto;
import com.bjsxt.mapper.MenuMapper;
import com.bjsxt.mapper.RoleMapper;
import com.bjsxt.service.MenuService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * @author Administrator
 * @title: MenuServiceImpl
 * @projectName open-his
 * @description: TODO
 * @date 2020/12/29 0029
 */
@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private RoleMapper roleMapper;

    /*
     * 查询菜单信息
     * 如果用户是超级管理员用户，查询所有菜单和所有权限
     * */
    @Override
    public List<Menu> selectMenuTree(boolean isAdmin, SimpleUser simpleUser) {
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(Menu.COL_STATUS, Constants.STATUS_TRUE);
        queryWrapper.in(Menu.COL_MENU_TYPE, Constants.MENU_TYPE_M, Constants.MENU_TYPE_C);
        queryWrapper.orderByDesc(Menu.COL_PARENT_ID);
        if (isAdmin) {
            return this.menuMapper.selectList(queryWrapper);
        } else {
//            此处先做全查询，等RBAC功能做完后再来修改
            return this.menuMapper.selectMenuListByUserId(simpleUser.getUserId());
        }
    }

    /**
     * 根据条件查询所有菜单
     *
     * @param menuDto
     * @return
     */
    @Override
    public List<Menu> listAllMenus(MenuDto menuDto) {
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(menuDto.getMenuName()), Menu.COL_MENU_NAME, menuDto.getMenuName());
        queryWrapper.eq(StringUtils.isNotBlank(menuDto.getStatus()), Menu.COL_STATUS, menuDto.getStatus());
        return this.menuMapper.selectList(queryWrapper);
    }

    /**
     * 根据id查询菜单和权限
     *
     * @param menuId
     * @return
     */
    @Override
    public Menu getOne(Long menuId) {
        return this.menuMapper.selectById(menuId);
    }

    /**
     * 添加菜单或权限
     *
     * @param menuDto
     * @return
     */
    @Override
    public int addMenu(MenuDto menuDto) {
        Menu menu = new Menu();
        BeanUtil.copyProperties(menuDto, menu);
//        设置创建时间创建人
        menu.setCreateBy(menuDto.getSimpleUser().getUserName());
        menu.setCreateTime(DateUtil.date());
        return this.menuMapper.insert(menu);
    }

    /**
     * 修改菜单或权限
     *
     * @param menuDto
     * @return
     */
    @Override
    public int updateMenu(MenuDto menuDto) {
        Menu menu = new Menu();
        BeanUtil.copyProperties(menuDto, menu);
//        设置修改人和修改时间
        menu.setUpdateBy(menuDto.getSimpleUser().getUserName());
        menu.setUpdateTime(DateUtil.date());
        return this.menuMapper.updateById(menu);
    }

    /**
     * 根据id删除菜单或权限
     *
     * @param menuId
     * @return
     */
    @Override
    public int deleteMenuById(Long menuId) {
//        先删除role_menu中间表的数据
        this.roleMapper.deleteRoleMenuByMenuIds(Arrays.asList(menuId));
        return this.menuMapper.deleteById(menuId);
    }

    /**
     * 根据菜单id判断菜单是否有子节点
     *
     * @param menuId
     * @return
     */
    @Override
    public boolean hasChildByMenuId(Long menuId) {
        return this.menuMapper.queryChildCountByMenuId(menuId) > 0L ? true : false;
    }

    /**
     * 根据角色id查询菜单权限id数据
     *
     * @param roleId
     * @return
     */
    @Override
    public List<Long> getMenusIdsByRoleId(Long roleId) {
        return this.menuMapper.queryMenuIdsByRoleId(roleId);
    }
}
